<?php

namespace app\controller\mobile;

use app\common\model\Authentication;
use app\common\model\User;
use app\common\service\AuthenticationService;
use app\common\util\Result;
use app\controller\mobile\MobileAuthorizedController;
use think\annotation\route\Group;
use think\annotation\route\Route;

#[Group('mobile/authentication')]
class AuthenticationController extends MobileAuthorizedController
{
    /**
     * 实名认证记录
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    #[Route('GET|POST', 'detail')]
    public function detail()
    {
        $rule = [
            'user_id|用户ID' => 'require|number',
        ];
        ['user_id' => $userId] = $param = \request()->param();

        $this->validate($param, $rule);
        $res = (new AuthenticationService(app()))->detailByUserId($userId);
        return Result::jsonByResult($res);
    }

    /**
     * 更新实名认证信息
     * @return \think\Response\Json
     */
    #[Route('POST', 'update')]
    public function update(User $user)
    {
        $param = request()->param();
        $type = input("type", Authentication::TYPE_FARMER);
        //更新实名认证信息
        $rule = [
            //'user_id|用户ID' => 'require|number',
            'real_name|姓名' => 'require',
            'phone|手机号' => 'require|number|length:11',
            'head_img|照片' => 'require',
            'area|区域' => 'require',
            'id_card|身份证号' => 'require',
            'type|认证类型' => 'require',
            'id_card_front|身份证正面' => 'require',
            'id_card_back|身份证背面' => 'require',
        ];

        $this->validate($param, $rule);
        $param["user_id"] = $user->id;
        $res = (new AuthenticationService(app()))->update($param);
        return Result::jsonByResult($res);

    }

}